﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDateTime>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),m_grpcInterface(nullptr)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_connect_btn_clicked()
{
    if(m_grpcInterface==nullptr){
        m_grpcInterface = new GrpcInterface;
        connect(m_grpcInterface, &GrpcInterface::streamConnected,
                this, &MainWindow::onStreamConnected);

        connect(m_grpcInterface, &GrpcInterface::streamClosed,
                this, &MainWindow::onStreamClosed);

        connect(m_grpcInterface, &GrpcInterface::streamMessage,
                this, &MainWindow::onStreamMessage);

        connect(m_grpcInterface, &GrpcInterface::streamError,
                this, &MainWindow::onStreamError);
    }
    m_grpcInterface->connectToServer(ui->lineEdit->text().toStdString());
}


void MainWindow::on_send_sync_msg_btn_clicked()
{
    if(m_grpcInterface==nullptr){
        return;
    }

    plugin_common::ConvertCoordinateRequest convertReq;
    convertReq.set_latitude(30.123456);
    convertReq.set_longitude(120.123456);
    convertReq.set_target_system("GCJ-02");

    plugin_common::CommonRequest req;
    req.set_request_id("convert_coordinate_001");
    req.set_plugin_type(plugin_common::GEO);
    req.set_method("ConvertCoordinate");


    google::protobuf::Any *any = req.mutable_payload();
    any->PackFrom(convertReq);

    m_grpcInterface->sendSyncRequest(req);
}


void MainWindow::on_send_async_msg_btn_clicked()
{
    if (!m_grpcInterface)
        return;

    // 生成唯一 request_id
    QString requestId = QString("async_%1").arg(QTime::currentTime().msecsSinceStartOfDay());

    plugin_common::CommonRequest req;
    req.set_plugin_type(plugin_common::CACHE);
    req.set_request_id(requestId.toStdString());
    req.set_method("LongAsyncTask");

    // payload 可由任意 Any 填充
    plugin_common::AsyncResultRequest asyncReq;
    asyncReq.set_request_id(requestId.toStdString());

    req.mutable_payload()->PackFrom(asyncReq);

    m_grpcInterface->sendStreamMessage(req);

    //    QMessageBox::information(this, QStringLiteral("异步提交"),
    //                             QStringLiteral("任务 %1 已提交").arg(requestId));
}

#include <QDebug>

void MainWindow::on_pressure_test_btn_clicked()
{
    if (!m_grpcInterface)
        return;

    const int count = 5000;
    qDebug() << QStringLiteral("开始压测：") << count << QStringLiteral("条消息");

    for (int i = 0; i < count; ++i)
    {
        plugin_common::CommonRequest req;
        req.set_plugin_type(plugin_common::CACHE);
        req.set_request_id(QString("pressure_%1").arg(i).toStdString());
        req.set_method("TestPressure");

        m_grpcInterface->sendStreamMessage(req);
    }

}

void MainWindow::onStreamConnected()
{
    ui->textEdit->append(QStringLiteral("已连接 gRPC 服务器"));
}

void MainWindow::onStreamClosed()
{
    ui->textEdit->append(QStringLiteral("gRPC 流已关闭"));
}

void MainWindow::onStreamMessage(QByteArray data)
{
    plugin_common::CommonResponse resp;
    resp.ParseFromArray(data.data(), data.size());

    QString info = QStringLiteral("收到响应:\nID=%1\nCode=%2\nMsg=%3")
            .arg(resp.request_id().c_str())
            .arg(resp.code())
            .arg(resp.message().c_str());

    ui->textEdit->append(info);

    // qDebug() << "📩 Stream Resp:" << info;

}

void MainWindow::onStreamError(QString msg)
{
    ui->textEdit->append(QStringLiteral("gRPC 错误: ") + msg);
}

